package Interview.baidu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: leiwenkang
 * @date: 2020/7/11
 * @time: 18:05
 */
public class Test01多个数组的笛卡尔积 {

    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("a1", "a2", "a3");
        List<String> list2 = Arrays.asList("b1", "b2");
        List<String> list3 = Arrays.asList("c1", "c2", "c3");
        List<List<String>> list = new ArrayList<>();
        list.add(list1);
        list.add(list2);
        list.add(list3);

        List<String> result = printMultiArrayDical(list);
        System.out.println(result);
    }

    public static List<String> printMultiArrayDical(List<List<String>> list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        return getMultiArrayDical(list, 0, list.get(0));
    }


    private static List<String> getMultiArrayDical(List<List<String>> list, int index, List<String> result) {
        if (index >= list.size()) {
            return result;
        }
        ArrayList<String> temp = new ArrayList<>();
        for (int i = 0; i < list.get(index).size(); i++) {
            if (index == 0) {
                temp.add(list.get(index).get(i));
            } else {
                for (int j = 0; j < result.size(); j++) {
                    temp.add(result.get(j) + list.get(index).get(i));
                }
            }
        }
        return getMultiArrayDical(list, index + 1, temp);
    }
}
